package patrol.mobile.com.mobilepatrol.activity.security.emergency;

import android.support.v7.widget.SearchView;
import android.view.View;
import android.widget.Toast;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.activity.CommonListWithSearchActivity;
import patrol.mobile.com.mobilepatrol.activity.security.ConsTitles;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.EmergencyEnsure1ListAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.Ensure1Bean;
import patrol.mobile.com.mobilepatrol.bean.ProtectCollectionBean;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;

/**
 * Created by fuzhen.li on 2018/9/18.
 */
public class EmergencyEnsure1Activity extends CommonListWithSearchActivity {
    @Override
    protected BaseQuickAdapter initAdapter() {
        return new EmergencyEnsure1ListAdapter(this);
    }

    @Override
    protected String initTitleName() {
        return ConsTitles.EMERGENCY_ENSURE_1;
    }

    @Override
    public void configAdapter() {
        super.configAdapter();
//        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
//            @Override
//            public void onItemClick(View view, int position) {
//                startActivity(EmergencyEnsure2Activity.class);
//            }
//        });
        requestData(searchView.getQuery().toString());
        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                requestData(searchView.getQuery().toString());
            }
        });
    }

    private void requestData(String baseName) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.EMERGENCY_ENSURE_1_LIST);
        HashMap<String, String> map = new HashMap<>();
        map.put("baseName", baseName);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();
                JsonArray listArray = jo.getAsJsonArray("list");
                List<Ensure1Bean> ensure1BeanList = GsonUtil.jsonToList(listArray.toString(), Ensure1Bean.class);
                mAdapter.updateItems(ensure1BeanList);
            }
        });
    }


}
